<?php
namespace Common\Mysql;
class ParseMysql extends CommonMysql {

    public function addParse($data) {
        $title = v($data,'title');
        verify($title,'notEmpty','解析器名称不能为空');

        $source_name = v($data,'source_name');
        verify($source_name,'notEmpty','来源名称不能为空');

        $source_url = v($data,'source_url');
        verify($source_url,'notEmpty','来源地址不能为空');
        verify($source_url,'isUrl','非法来源地址');

        $config = v($data,'config');
        verify($config,'notEmpty','网站配置不能为空');
        $config = trim($config);
        if(!json_decode($config,true)) {
            E('非法配置');
        }

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        if($this->where("`title`='{$title}'")->count()) {
            E('解析器已经存在');
        }

        $id = $this->addRecord(array(
            'title'         =>  $title,
            'source_name'   =>  $source_name,
            'source_url'    =>  $source_url,
            'config'        =>  $config,
            'createtime'    =>  NOW_TIME,
            'status'        =>  $status
        ),false,'解析器添加失败');

        return $id;
    }

    public function updateParse($data) {
        $id = v($data,'id');
        verify($id,'notEmpty','解析器ID不能为空');
        verify($id,'isID','非法解析器ID');

        $title = v($data,'title');
        verify($title,'notEmpty','解析器名称不能为空');

        $source_name = v($data,'source_name');
        verify($source_name,'notEmpty','来源名称不能为空');

        $source_url = v($data,'source_url');
        verify($source_url,'notEmpty','来源地址不能为空');
        verify($source_url,'isUrl','非法来源地址');

        $config = v($data,'config');
        verify($config,'notEmpty','网站配置不能为空');
        if(!json_decode($config)) {
            E('非法配置');
        }

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        if($this->where("`title`='{$title}' AND `id` <> {$id}")->count()) {
            E('解析器已经存在');
        }

        $this->updateRecord(array(
            'id'            =>  $id,
            'title'         =>  $title,
            'source_name'   =>  $source_name,
            'source_url'    =>  $source_url,
            'config'        =>  $config,
            'status'        =>  $status,
        ),false,'解析器添加失败');

        return $id;
    }
}
?>
